package net.example.model.common;


import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import io.netty.buffer.ByteBuf;


/**
 * Created by ZN on 2017/1/11.
 */

public class RichChatMessage {
    private MessageColor color = new MessageColor();
    private MessageFont font;
    private HashMap<Integer, NonText> nonTextHashMap = new HashMap<>();
    private String text=" ";
    public Date TransferTime = new Date();

    public MessageColor getColor() {
        return color;
    }

    public void setColor(MessageColor color) {
        this.color = color;
    }

    public MessageFont getFont() {
        return font;
    }

    public void setFont(MessageFont font) {
        this.font = font;
    }
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }


    public HashMap<Integer, NonText> getNonTextHashMap() {
        return nonTextHashMap;
    }

    public void setNonTextHashMap(HashMap<Integer, NonText> nonTextHashMap) {
        this.nonTextHashMap = nonTextHashMap;
    }

    public void sortByKey()
    {
        Set<Map.Entry<Integer, NonText>> entries = nonTextHashMap.entrySet();

        //存入到list，用来排序
        ArrayList<Map.Entry<Integer, NonText>> list = new ArrayList<>(entries);
        //调用Collections排序
        //第一个参数：集合
        //第二个参数：排序核心方法
        Collections.sort(list, new Comparator<Map.Entry<Integer, NonText>>() {
            @Override
            public int compare(Map.Entry<Integer, NonText> o1, Map.Entry<Integer, NonText> o2) {
                //倒序
                return o1.getKey().compareTo(o2.getKey());
            }
        });

        //创建有序集合LinkedHashMap，存放数据
        LinkedHashMap<Integer, NonText> linkedHashMap = new LinkedHashMap<>();
        //遍历list
        for (Map.Entry<Integer, NonText> entry : list) {
            //将数据存到linkedHashMap
            linkedHashMap.put(entry.getKey(),entry.getValue());
        }
        nonTextHashMap=linkedHashMap;
    }

    public void deserialize(ByteBuf buffer) throws Exception {
        buffer.readInt();
        color = new MessageColor();
        color.deserialize(buffer);
        font = new MessageFont();
        font.deserialize(buffer);
        int count = buffer.readInt();
        nonTextHashMap = new HashMap<>();
        for (int i = 0; i < count; i++) {
            int key = buffer.readInt();
            NonText nonText = new NonText();
            nonText.deserialize(buffer);
            nonTextHashMap.put(key, nonText);
        }
        text = SerializeUtils.readStrIntLen(buffer);
        this.sortByKey();
//        long msecs = buffer.readLong() - 8*3600*1000;
//        this.TransferTime.setTime(msecs);
    }

    public byte[] serialize() throws Exception {
        this.sortByKey();
        ByteBuf body = BufferUtils.newBuffer();
        int totalLen = 0;
        body.writeInt(totalLen);
        byte[] colorBytes = color.serialize();
        body.writeBytes(colorBytes);
        totalLen += colorBytes.length;
        if (font != null) {
            byte[] fontBytes = font.serialize();
            body.writeBytes(fontBytes);
            totalLen +=4+ fontBytes.length;
        } else {
            body.writeInt(-1);
            totalLen += 4;
        }
        body.writeInt(nonTextHashMap.size());
        totalLen += 4;
        for (Map.Entry<Integer, NonText> entry : nonTextHashMap.entrySet()) {
            body.writeInt(entry.getKey());
            totalLen += 4;
            byte[] entryBytes = entry.getValue().serialize();
            body.writeBytes(entryBytes);
            totalLen += entryBytes.length;
        }
        byte[] textBytes = this.text.getBytes(StandardCharsets.UTF_8);
        body.writeInt(textBytes.length);
        totalLen += 4;
        body.writeBytes(textBytes);
        totalLen += textBytes.length;
//        body.writeLong(this.TransferTime.getTime() + 8*3600*1000);
//        totalLen += 8 ;
        body.resetWriterIndex();
        body.writeInt(totalLen);

        byte[] result = new byte[totalLen + 4];
        System.arraycopy(body.array(),0,result,0,result.length);
        return result;
    }
}
